package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.model.BaseAttrInfo;
import com.atguigu.gmall.product.model.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service //将对象放到ioc容器
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper,BaseAttrInfo> implements BaseAttrInfoService {
    @Autowired
    private BaseAttrValueService baseAttrValueService;



    /**
     * 根据多个三级分类id,查询当前分类包含平台属性姓名列表
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        //1.获取持久层对象自定义的查询方法
        BaseAttrInfoMapper baseAttrInfoMapper = this.getBaseMapper();
        //2.执行查询
        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoMapper.getAttrInfoList(category1Id,category2Id,category3Id);
        return baseAttrInfoList;
    }

    /**
     * 保存/修改平台属性或平台属性列表
     * @param baseAttrInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class) //事务回滚
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
    //1.判断是否是否是否有平台属性id提交,有-修改,没有-新增
        if(baseAttrInfo.getId() == null){
            //2.新增到baseAttrInfo表
            this.save(baseAttrInfo);
            //2.1批量新增到新增到baseAttrInfo表
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if(!CollectionUtils.isEmpty(attrValueList)){
                attrValueList.stream().forEach(baseAttrValue -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                //3.批量保存
                baseAttrValueService.saveBatch(attrValueList);
            }
        }else{
            //4.修改
            this.updateById(baseAttrInfo);
            //先删除所有平台属性值在跟新
            LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<BaseAttrValue>();
            queryWrapper.eq(BaseAttrValue::getAttrId,baseAttrInfo.getId());
            //调移除方法
            baseAttrValueService.remove(queryWrapper);
            //更新平台属性
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if(!CollectionUtils.isEmpty(attrValueList)){
                attrValueList.stream().forEach(baseAttrValue -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                //批量保存
                baseAttrValueService.saveBatch(attrValueList);
            }
        }
    }

    /**
     * 根据平台属性id,查询属性列表
     * @param attrId
     * @return
     */
    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<BaseAttrValue>();
        queryWrapper.eq(BaseAttrValue::getAttrId,attrId);
        return baseAttrValueService.list(queryWrapper);
    }
}